﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace VDataAdapter
{
    class TimeInVariantAsserter
    {
        /// <summary>
        /// This Assert 2 string with contains time pattern e.g. hh:mm:ss
        /// by replace the pattern with xx so that the time value in the message
        /// is unimportant
        /// </summary>
        /// <param name="expected"></param>
        /// <param name="actual"></param>
        public static void AreEqual(string expected, string actual)
        {
            Assert.AreEqual(ReplaceTimeValue(expected), ReplaceTimeValue(actual));
        }

        /// <summary>
        /// This Assert 2 string with contains time pattern e.g. hh:mm:ss
        /// by replace the pattern with xx so that the time value in the message
        /// is unimportant
        /// </summary>
        /// <param name="expected"></param>
        /// <param name="actual"></param>
        public static void AreEqual(string expected, string actual, string message)
        {
            Assert.AreEqual(ReplaceTimeValue(expected), ReplaceTimeValue(actual), message);
        }

             /// <summary>
        /// Find time format and replace with xxx
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        private static string ReplaceTimeValue(string msg)
        {
            for (int i = 0; i < msg.Length - 6; i++)
            {
                if (msg[i + 3] == ':' && msg[i] == '|' && msg[i + 6] == ':')
                {
                    msg = ReplaceSubString(msg, i + 1, 8, 'x');
                }
            }
            
            return msg;
        }

        /// <summary>
        /// Replace all characters in substring with character x
        /// </summary>
        /// <returns></returns>
        private static string ReplaceSubString(string msg, int index, int length, char x)
        {
            string insertStr = new string(x, length);

            msg = msg.Substring(0, index) + insertStr + msg.Substring(index + length);
            
            return msg;
        }
    }
}
